// TypeScript file
class Bomb extends egret.Sprite
{
    private speed: number = 0;
    private angle: number = 0;
    public lastX: number = 0;
    public lastY: number = 0;
    private sp: egret.Bitmap;
    constructor()
    {
        super();
        this.sp = ResourceUtils.createBitmapByName("bomb_png");
        this.addChild(this.sp);
        this.sp.x = this.sp.width / 2;
        this.sp.rotation = 90;
        this.speed = 30;
        this.visible = false;
        egret.Ticker.getInstance().register(this.onFrame, this);
    }

    public move(): void
    {
        this.rotation = Math.atan2(this.lastY - this.y, this.lastX - this.x) * 180 / Math.PI;
        this.angle = this.rotation;
    }

    private onFrame(): void
    {
        if (!this.visible) return;

        let rad: number = this.angle / 180 * Math.PI;
        this.x += this.speed * Math.cos(rad);
        this.y += this.speed * Math.sin(rad);

        let distance: number = Math.sqrt(Math.pow(this.x - this.lastX, 2) + Math.pow(this.y - this.lastY, 2));
        
        if (distance < 15)
        {
            this.visible = false;
        }
    }

    public dispose(): void
    {
        egret.Ticker.getInstance().unregister(this.onFrame, this);
        this.removeChildren();
        this.sp = null;
    }
}